<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退货申请单编号" prop="saleBackRequestCode">
        <el-input
          v-model="queryParams.saleBackRequestCode"
          placeholder="请输入退货申请单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售出库单编号" prop="saleOutCode">
        <el-input
          v-model="queryParams.saleOutCode"
          placeholder="请输入销售出库单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总价" prop="total">
        <el-input
          v-model="queryParams.total"
          placeholder="请输入总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
                        v-model="queryParams.createDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="updateDate">
        <el-date-picker clearable
                        v-model="queryParams.updateDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <AddSaleBackRequest @updList="getList" ref="asbr"></AddSaleBackRequest>
        <UpdateSaleBackRequest :updData="this.form" @updList="getList" ref="usbq"></UpdateSaleBackRequest>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xiaoshou:saleBackRequest:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="saleBackRequestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="退货申请单编号" width="200" show-overflow-tooltip align="center"
                       prop="saleBackRequestCode">
        <template slot-scope="scope">
          <SaleBackRequestView :viewData="scope.row"/>
        </template>
      </el-table-column>
      <el-table-column label="销售出库单编号" width="200" show-overflow-tooltip align="center" prop="saleOutCode"/>
      <el-table-column label="总价" align="center" prop="total"/>
      <el-table-column label="总数量" align="center" prop="totalNum"/>
      <el-table-column label="审核时间" align="center" prop="passDate"/>
      <el-table-column label="客户名称" align="center" prop="cusName"/>
      <el-table-column label="审核人" align="center" prop="userId"/>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status == 0 ? "待审核" : scope.row.status == 1 ? "审核通过" : "已驳回" }}
        </template>
      </el-table-column>
      <el-table-column label="已关闭" align="center" prop="isClose">
        <template slot-scope="scope">
          {{ scope.row.isClose == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column label="已作废" align="center" prop="isCancel">
        <template slot-scope="scope">
          {{ scope.row.isCancel == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy">
        <template slot-scope="scope">
          {{ scope.row.updateBy == 0 ? "" : scope.row.updateBy }}
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateDate" width="180">
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.status==1||scope.row.status==2||scope.row.isCancel==1||scope.row.isClose==1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCheck(scope.row.id,1)"
            v-hasPermi="['xiaoshou:saleBackRequest:edit']"
          >审核
          </el-button>
          <el-button
            :disabled="scope.row.status==1||scope.row.status==2||scope.row.isCancel==1||scope.row.isClose==1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xiaoshou:saleBackRequest:edit']"
          >修改
          </el-button>
          <el-button
            :disabled="scope.row.status==1||scope.row.status==2||scope.row.isCancel==1||scope.row.isClose==1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xiaoshou:saleBackRequest:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售退货申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left">
        <el-form-item label="退货申请单编号" prop="saleBackRequestCode">
          <el-input v-model="form.saleBackRequestCode" placeholder="请输入退货申请单编号"/>
        </el-form-item>
        <el-form-item label="销售出库单编号" prop="saleOutCode">
          <el-input v-model="form.saleOutCode" placeholder="请选择销售出库单" @blur="selStockOut"/>
        </el-form-item>
        <el-form-item label="总价" prop="total">
          <el-input v-model="form.total" placeholder="请输入总价"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSaleBackRequest,
  getSaleBackRequest,
  delSaleBackRequest,
  addSaleBackRequest,
  updateSaleBackRequest
} from "@/api/xiaoshou/saleBackRequest";
import SaleBackRequestView from "@/views/xiaoshou/saleBackRequest/SaleBackRequestView.vue";
import AddSaleBackRequest from "@/views/xiaoshou/saleBackRequest/AddSaleBackRequest.vue";
import UpdateSaleBackRequest from "@/views/xiaoshou/saleBackRequest/UpdateSaleBackRequest.vue";
import request from "@/utils/request"

export default {
  name: "SaleBackRequest",
  components: {UpdateSaleBackRequest, SaleBackRequestView, AddSaleBackRequest},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售退货申请表格数据
      saleBackRequestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        saleBackRequestCode: null,
        saleOutCode: null,
        total: null,
        userId: null,
        status: null,
        createBy: null,
        createDate: null,
        updateBy: null,
        updateDate: null,
      },
      //入库单列表

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        saleBackRequestCode: [
          {required: true, message: "退货申请单编号不能为空", trigger: "blur"}
        ],
        saleOutCode: [
          {required: true, message: "销售出库单编号不能为空", trigger: "blur"}
        ],
        total: [
          {required: true, message: "总价不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "审核状态不能为空", trigger: "change"}
        ],
        createBy: [
          {required: true, message: "创建人不能为空", trigger: "blur"}
        ],
        createDate: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        isDel: [
          {required: true, message: "是否删除(0:未删除 1:删除)不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selStockOut() {

    },
    //审核
    handleCheck(id, status) {
      // const ids = this.ids;
      const ids = [id];
      let userId = this.$store.getters.name;
      request({
        url: '/xiaoshou/saleBackRequest/checkSaleRequest/' + ids + "/" + status + "/" + userId,
        method: "post"
      }).then(res => {
        if (res.code == 200) {
          let message = "";
          if (status == 1) {
            message = "审核通过完成！";
          } else {
            message = "驳回完成！";
          }
          this.$notify({
            type: "success",
            message: message,
            title: "审核"
          })
          this.getList();
        }

      });
    },
    /** 查询销售退货申请列表 */
    getList() {
      this.loading = true;
      listSaleBackRequest(this.queryParams).then(response => {
        this.saleBackRequestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        saleBackRequestCode: null,
        saleOutCode: null,
        total: null,
        userId: null,
        status: null,
        createBy: null,
        createDate: null,
        updateBy: null,
        updateDate: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售退货申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSaleBackRequest(id).then(response => {
        this.form = response.data;
        this.$refs.usbq.showUpd();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSaleBackRequest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSaleBackRequest(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除销售退货申请编号为"' + ids + '"的数据项？').then(function () {
        return delSaleBackRequest(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xiaoshou/saleBackRequest/export', {
        ...this.queryParams
      }, `saleBackRequest_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
